package Test;

import java.io.*;
import java.net.*;

public class ServerThread extends Thread{
    Socket socket;
    DataOutputStream out=null;
    DataInputStream in=null;
    String s=null;
    ServerThread(Socket t) {
        socket=t;
        try {  out=new DataOutputStream(socket.getOutputStream());
            in=new DataInputStream(socket.getInputStream());
        }
        catch (IOException e){}
    }
    @Override
    public void run() {
        while(true) {
            try{  String s=in.readUTF();
                //堵塞状态，除非读取到信息
                System.out.println(s);
                s=StringToArithmetic.infixToSuffix(s);
                System.out.println(s);
                String str [] = new String[100];
                int i,j=0;
                System.out.printf("%d\n",s.length());
                for (i = 0;i <s.length(); i++){
                    str [i] = "";
                }
                for (i = 0; i<s.length(); i++){
                    if (s.charAt(i) == '1'&&s.charAt(i+1) == '0'){
                        str [j] += s.charAt(i);
                    }
                    else{
                        str [j] += s.charAt(i);
                        j++;
                    }
                }
                for(i=0;i<s.length();i++){
                    System.out.printf("%s",str[i]);
                }
                Solution sl = new Solution();
                if(s.length()!=0&&str[0]!=null){
                    Rational ra = sl.evalRPN(str);
                    out.writeUTF(ra.toString());
                }
                out.writeUTF(s);
            }
            catch (IOException e) {
                System.out.println("客户离开");
                return;
            }
        }
    }
}
